package com.tcm.controller.backend;

import com.tcm.common.enums.ResponseEnum;
import com.tcm.config.AppProperties;
import com.tcm.entity.User;
import com.tcm.entity.dto.param.UserLoginParam;
import com.tcm.entity.resp.R;
import com.tcm.exception.UserNotExistException;
import com.tcm.exception.UserTypeNotInvalidException;
import com.tcm.service.AuthService;
import com.tcm.utils.AccessTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;

/**
 * @author Guqier
 * @version 1.0
 * @date 2023/7/1 22:21
 */
@Slf4j
@RestController()
@RequestMapping("/pc/auth")
public class AuthController {

    @Autowired
    private AuthService authService;

    /**
     * PC端用户登录
     * @param userLoginParam 用户数据封装对象
     * @return 登录结果
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public R login(@RequestBody @Valid UserLoginParam userLoginParam){
        String code = userLoginParam.getCode();
        if (!authService.verifyAuthCode(userLoginParam.getUuid(), code)) {
            return R.fail(ResponseEnum.ERROR_AUTH_CODE);
        }
        User user = new User();
        BeanUtils.copyProperties(userLoginParam, user, "code");
        Map<String, String> resMap = null;
        try {
            resMap = authService.login(user, AppProperties.BACKEND_LOGIN_ALLOW_USER_TYPE);
        }catch (UserNotExistException e){
            return R.fail(ResponseEnum.USER_LOGIN_ERROR);// 用户不存在
        }catch (UserTypeNotInvalidException e){
            return R.fail(205, e.getMessage());// 学生登录禁止
        }
        if (resMap == null) {
            return R.fail(ResponseEnum.USER_LOGIN_ERROR);// 账号或密码错误
        }
        return R.success(resMap);
    }

    /**
     * PC端用户登出
     * @param request http请求对象
     * @return 登出结果
     */
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    public R logout(HttpServletRequest request){
        if (authService.logout(AccessTokenUtil.getAccessToken(request))) {
            return R.success();
        }else {
            return R.fail(ResponseEnum.USER_NOT_LOGIN);
        }
    }

    /**
     * 刷新AccessToken
     * @param request http请求对象
     * @return 刷新后Token
     */
    @RequestMapping(value = "/refresh", method = RequestMethod.GET)
    public R refreshToken(HttpServletRequest request){
        Long userId = AccessTokenUtil.getUserId(request);
        String account = AccessTokenUtil.getUserAccount(request);
        log.info("刷新Token：{}", userId);
        Map<String, String> map = authService.refreshToken(userId, account, AccessTokenUtil.getAccessToken(request));
        return R.success(map);
    }

    /**
     * 获取登录验证码
     * @return 正常情况响应为null
     */
    @RequestMapping(value = "/code", method = RequestMethod.GET)
    public R authCode(){
        return R.success(authService.generateAuthCode());
    }
}
